<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\ArticleKind;
use app\admin\model\Sitemap;
class Other extends Common
{
	//附件管理
	function fujian(){
		return view();
	}
	//广告分类列表
	function adsKindList(){
		$adsList = db('ads_kind')->order('sort_order')->select();
		$this->assign('adsList',$adsList);
		return view('adsKindList');
	}

	//添加广告类别
	public function addAdsKind() {
		$data = input('post.');
		if (empty($data['kind_name'])) {
			$json['status'] = 0;
			$json['message'] = "广告分类名称不能为空！";
			echo json_encode($json);
			return;
		}
		$result = db('ads_kind')->insertGetId($data);
		if ($result) {
			$json['status'] = 1;
			$json['message'] = "广告添加成功！";
			/*********************记录日志***********************/
            addLog(session('user_name').'添加广告分类成功-'.$data['kind_name'].' [ ID:'.$result.' ]');
			echo json_encode($json);
			return;
		} else {
			$json['status'] = 0;
			$json['message'] = "广告添加失败！";
			echo json_encode($json);
			return;
		}
	}

	//编辑广告分类
	function EditAdsKind() {
		$data = input('post.');
		if ($data['id']) {
			if (empty($data['kind_name'])) {
				$json['status'] = 0;
				$json['message'] = "广告分类分类名称不能为空！";
				echo json_encode($json);
				return;
			}
			$result = db('ads_kind')->update($data);
			if ($result !== false) {
				$json['status'] = 1;
				$json['message'] = "广告分类修改成功！";
				/*********************记录日志***********************/
            	addLog(session('user_name').'修改广告分类成功-'.$data['kind_name'].' [ ID:'.$data['id'].' ]');
				echo json_encode($json);
				return;
			} else {
				$json['status'] = 0;
				$json['message'] = "广告分类修改失败！";
				echo json_encode($json);
				return;
			}
		} else {
			$json['status'] = 0;
			$json['message'] = "参数错误，请刷新页面！";
			echo json_encode($json);
			return;
		}
	}

	//删除广告类别
	function DelAdsKind() {
		$id = input('post.id');
		$find = db('ads_kind')->where(array('id'=>$id))->find();
		if ($find) {
			$result = db('ads_kind')->where(array('id'=>$id))->delete();
			if ($result) {
				$json['status'] = 1;
				$json['message'] = "广告分类删除成功！";
				/*********************记录日志***********************/
            	addLog(session('user_name').'删除广告分类成功-'.$find['kind_name'].' [ ID:'.$id.' ]');
				echo json_encode($json);
				return;
			} else {
				$json['status'] = 0;
				$json['message'] = "广告分类删除失败！";
				echo json_encode($json);
				return;
			}
		} else {
			$json['status'] = 0;
			$json['message'] = "参数错误，请刷新页面！";
			echo json_encode($json);
			return;
		}
	}

	//广告列表
	function adsList() {
		$cat_select = db('ads_kind')->order('sort_order')->select();
		$this->assign('cat_select',$cat_select);//广告栏目分类
		return view('adsList');
	}
	//异步加载ads列表
	function ajaxAdsList() {
		$get_data = input('get.');
		$data = input('post.');

		$p = empty($_GET['p']) ? 1 : $_GET['p'];
        $size = empty($_REQUEST['size']) ? 25 : $_REQUEST['size'];
        if ($data['kind_id'])
            $where['kid'] = $data['kind_id'];
		$count = db('ads')->where($where)->count();// 查询满足要求的总记录数
        $list = db('ads')->where($where)->order('sort_order')->paginate($size,$count);
        $page = $list->render();

        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$page);// 赋值分页输出
        $this->assign('total',$count);// 数据总数

		return view('ajaxAdsList');
	}
	function adsForm(){
		if(input('route.id')){
			$id = input('route.id');
			$info = db('ads')->find($id);
		}
		$this->assign('info',$info);
		//广告分类
        $cat_select = db('ads_kind')->select();
		$this->assign('cat_select',$cat_select);//广告栏目分类
		return view('adsForm');
	}
	//广告表单数据处理
	function AdsData($data) {
		$type = $data['tid']; 
		switch ($type) {
			case '0'://HTML代码
				$content['script'] = $data['txt_script'];
				break;

			case '1'://文字广告
				$content['txt'] = $data['txt_txt'] ? $data['txt_txt'] : '';
				$content['link'] = $data['txt_link'];
				$content['style'] = $data['txt_style'] ? $data['txt_style'] : '';
				break;
			
			case '2'://图片广告
				$content['image'] = $data['img_img'];
				$content['link'] = $data['img_link'];
				$content['width'] = $data['img_width'];
				$content['height'] = $data['img_height'];
				$content['alt'] = $data['img_alt'];
				break;

			case '3'://Flash广告
				$content['swf'] = $data['flash_swf'];
				$content['width'] = $data['flash_width'];
				$content['height'] = $data['flash_height'];
				break;

			case '4'://幻灯片
				$s_img = $data['slide_img'];
				$s_link = $data['slide_link'];
				$s_alt = $data['slide_alt'];
				$s_order = $data['slide_order'];
				for ($i=0;$i<count($s_img);$i++) {
					$content[] = array(
						'image'=>$s_img[$i],
						'width'=>$data['slide_width'],
						'height'=>$data['slide_height'],
						'link'=>$s_link[$i],
						'alt'=>$s_alt[$i],
						'order'=>$s_order[$i],
					);
				}

				//排序开始，数组中根据某个元素排序
	            $sort = array(
	             'direction' => 'SORT_ASC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
	             'field'     => 'order',    //排序字段
	            );
	            $arrSort = array();
	            foreach($content AS $uniqid => $row){
	                foreach($row AS $key=>$value){
	                    $arrSort[$key][$uniqid] = $value;
	                }
	            }
	            if($sort['direction']){
	                array_multisort($arrSort[$sort['field']], constant($sort['direction']), $content);
	            }
	            //排序结束
				break; 
		}
		$checkData['id'] = $data['id']; 
		$checkData['title'] = $data['title']; 
		$checkData['sub_title'] = $data['sub_title']; 
		$checkData['kid'] = $data['kid']; 
		$checkData['tid'] = $data['tid']; 
		$checkData['is_hide'] = empty($data['is_hide']) ? 0 : 1;
		$checkData['sort_order'] = empty($data['sort_order'])?98:$data['sort_order']; 
		$checkData['content'] = json_encode($content); 
		return $checkData;
	}
	//广告表单处理
	function adsEditHandle(){
		if(request()->isPost()){
			$data = $this->AdsData(input('post.'));
			// dump($data);exit;
			$id = $data['id'];
			if($id){
				unset($data['id']);
				$res = db('ads')->where(array('id'=>$id))->update($data);
				if($res !== false){
					$json = array('status'=>1,'message'=>'修改广告内容成功！');
					/*********************记录日志***********************/
            		addLog(session('user_name').'修改广告成功-'.$data['title'].' [ ID:'.$id.' ]');
				}else{
					$json = array('status'=>0,'message'=>'修改广告内容失败！');
				}
				echo json_encode($json);exit;
			}else{
				$res = db('ads')->insertGetId($data);
				if($res){
					$json = array('status'=>1,'message'=>'添加广告成功！');
					/*********************记录日志***********************/
            		addLog(session('user_name').'添加广告成功-'.$data['title'].' [ ID:'.$res.' ]');
				}else{
					$json = array('status'=>0,'message'=>'添加广告失败！');
				}
				echo json_encode($json);exit;
			}
		}
	}
	// 广告表单-删除处理
	function adsDel() {
		$data = input('post.');//数据处理
		$temp = db('ads')->where(array('id'=>$data['id']))->find();
		$result = db('ads')->where(array('id'=>$data['id']))->delete();
		if ($result !== false) {
			$json['status'] = 1;
			$json['message'] = '删除广告成功！';
			/*********************记录日志***********************/
            addLog(session('user_name').'删除广告成功-'.$temp['title'].' [ ID:'.$data['id'].' ]');
			echo json_encode($json);
			return;
		} else {
			$json['status'] = 0;
			$json['message'] = '删除广告失败！';
			echo json_encode($json);
			return;
		}
	}

	//友情链接类型
	public function linkKind() {
		$adsList = db('link_kind')->select();
		$this->assign('adsList',$adsList);
		return view('linkKind');
	}
	//添加友情链接类别
	public function addLinkKind() {
		$data = input('post.');
		if (empty($data['kind_name'])) {
			$json['status'] = 0;
			$json['message'] = "友情链接分类名称不能为空！";
			echo json_encode($json);
			return;
		}
		$result = db('link_kind')->insertGetId($data);
		if ($result) {
			$json['status'] = 1;
			$json['message'] = "友情链接分类添加成功！";
			/*********************记录日志***********************/
            addLog(session('user_name').'添加友情链接分类成功-'.$data['kind_name'].' [ ID:'.$result.' ]');
			echo json_encode($json);
			return;
		} else {
			$json['status'] = 0;
			$json['message'] = "友情链接分类添加失败！";
			echo json_encode($json);
			return;
		}
	}
	//编辑友情链接分类
	function EditLinkKind() {
		$data = input('post.');
		if ($data['id']) {
			if (empty($data['kind_name'])) {
				$json['status'] = 0;
				$json['message'] = "友情链接分类名称不能为空！";
				echo json_encode($json);
				return;
			}
			$result = db('link_kind')->update($data);
			if ($result !== false) {
				$json['status'] = 1;
				$json['message'] = "友情链接分类修改成功！";
				/*********************记录日志***********************/
            	addLog(session('user_name').'修改友情链接分类成功-'.$data['kind_name'].' [ ID:'.$data['id'].' ]');
				echo json_encode($json);
				return;
			} else {
				$json['status'] = 0;
				$json['message'] = "友情链接分类修改失败！";
				echo json_encode($json);
				return;
			}
		} else {
			$json['status'] = 0;
			$json['message'] = "参数错误，请刷新页面！";
			echo json_encode($json);
			return;
		}
	}
	//删除友情链接分类
	function DelLinkKind() {
		$id = input('post.id');
		$find = db('link_kind')->where(array('id'=>$id))->find();
		if ($find) {
			$result = db('link_kind')->where(array('id'=>$id))->delete();
			if ($result) {
				$json['status'] = 1;
				$json['message'] = "友情链接分类删除成功！";
				/*********************记录日志***********************/
            	addLog(session('user_name').'删除友情链接分类成功-'.$find['kind_name'].' [ ID:'.$id.' ]');
				echo json_encode($json);
				return;
			} else {
				$json['status'] = 0;
				$json['message'] = "友情链接分类删除失败！";
				echo json_encode($json);
				return;
			}
		} else {
			$json['status'] = 0;
			$json['message'] = "参数错误，请刷新页面！";
			echo json_encode($json);
			return;
		}
	}
	//友情链接列表
	function linkList() {
		$cat_select = db('link_kind')->select();
		$this->assign('cat_select',$cat_select);//友情链接栏目分类
		return view('linkList');
	}
	//异步加载ads列表
	function ajaxLinkList() {
		$get_data = input("get.");
		$data = input('post.');

		$p = empty($_GET['p']) ? 1 : $_GET['p'];
        $size = empty($_REQUEST['size']) ? 25 : $_REQUEST['size'];
        if ($data['kind_id'])
            $where['kid'] = $data['kind_id'];
		$count = db('link')->where($where)->count();// 查询满足要求的总记录数

        $list = db('link')->where($where)->order('id desc')->paginate($size,$count)->each(function($item,$key){
        	// $item['nickname'] = 'think';
            // $item['category'] = $cats[$item['kind_id']]['kind_name'];
        	return $item;
        });
        $page = $list->render();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$page);// 赋值分页输出
        $this->assign('total',$count);// 数据总数
		return view('ajaxLinkList');
	}
	function linkForm(){
		if(input('route.id')){
			$id = input('route.id');
			$info = db('link')->find($id);
		}
		$this->assign('info',$info);
		//广告分类
        $cat_select = db('link_kind')->select();
		$this->assign('cat_select',$cat_select);//广告栏目分类
		return view('linkForm');
	}
	function linkHandle(){
		if(request()->isPost()){
			$data = input('post.');
			// dump($data);exit;
			$data['sort_order'] = empty($data['sort_order'])?99:$data['sort_order'];
			unset($data['act']);
			$id = $data['id'];
			if($id){
				unset($data['id']);
				$res = db('link')->where(array('id'=>$id))->update($data);
				if($res !== false){
					$json = array('status'=>1,'message'=>'修改友情链接成功！');
					/*********************记录日志***********************/
            		addLog(session('user_name').'修改友情链接成功-'.$data['title'].' [ ID:'.$id.' ]');
				}else{
					$json = array('status'=>0,'message'=>'修改友情链接失败！');
				}
				echo json_encode($json);exit;
			}else{
				$res = db('link')->insertGetId($data);
				if($res){
					$json = array('status'=>1,'message'=>'添加友情链接成功！');
					/*********************记录日志***********************/
            		addLog(session('user_name').'添加友情链接成功-'.$data['title'].' [ ID:'.$res.' ]');
				}else{
					$json = array('status'=>0,'message'=>'添加友情链接失败！');
				}
				echo json_encode($json);exit;
			}
		}
	}
	// 友情链接表单-删除处理
	function linkDel() {
		$data = input('post.');//数据处理
		$find = db('link')->where(array('id'=>$data['id']))->find();
		$result = db('link')->where(array('id'=>$data['id']))->delete();
		if ($result !== false) {
			$json['status'] = 1;
			$json['message'] = '删除友情链接成功！';
			/*********************记录日志***********************/
            addLog(session('user_name').'删除友情链接成功-'.$find['title'].' [ ID:'.$find['id'].' ]');
			echo json_encode($json);
			return;
		} else {
			$json['status'] = 0;
			$json['message'] = '删除友情链接失败！';
			echo json_encode($json);
			return;
		}
	}

	function seokind(){
		$one = db('config')->where(array('inc_type'=>'baidu_token','name'=>'token'))->find();
		$this->assign('one',$one);
		return view('seo');
	}

	function saveToken(){
		$data['value'] = input('post.token');
		$data['inc_type'] = 'baidu_token';
		$data['name'] = 'token';
		$one = db('config')->where(array('inc_type'=>'baidu_token','name'=>'token'))->find();
		if($data['value']==''){
			$json = array('status'=>0,'message'=>'请输入Token！');
		}else{
			if($one){
				db('config')->where(array('id'=>$one['id']))->update($data);
			}else{
				db('config')->insertGetId($data);
			}
			$json = array('status'=>1,'message'=>'保存成功！');
			/*********************记录日志***********************/
            addLog(session('user_name').'保存Token成功-'.$data['value']);
		}
		echo json_encode($json);return;
	}

	function pushCon(){
		$time = input('post.time');
		$push = strtotime("-".$time." day");
		$pushtwo = date("Y-m-d H:i:s",strtotime("-".$time." day"));
		$nowtime = time();
		

		$one = db('config')->where(array('inc_type'=>'baidu_token','name'=>'token'))->find();
		if($one['value']==''){
			$json = array('status'=>0,'message'=>'请先设置Token！');
			echo json_encode($json);exit;
		}else{
			if($time==''){
				$json = array('status'=>0,'message'=>'请输入天数！');
				echo json_encode($json);exit;
			}else{
				$urls = array();
				$awhere[] = ['is_del','=',0]; 
				$awhere[] = ['is_hide','=',0]; 
				$awhere[] = ['add_time','>=',$pushtwo];
				$article = db('article')->field('article_id,title,kind_id')->where($awhere)->select(); 
				if(empty($article)){
					$json = array('status'=>0,'message'=>'近期没有新增的文章内容');
					echo json_encode($json);exit;
				}
				foreach ($article as $value) {
					$urls[] = 'http://'.$_SERVER['HTTP_HOST'].'/article/'.$value['article_id'].'.html';
				}

				$pwhere['is_del'] = 0;
				// $pwhere['is_on_sale'] = 1;
				// $pwhere['on_time'] = array('egt',$push);
				// $product = M('Goods')->field('goods_id,goods_name,cat_id')->where($pwhere)->select(); 
				// foreach ($product as $val) {
				// 	$urls[] = 'http://'.$_SERVER['HTTP_HOST'].U('Home/Index/product',array('id'=>$val['goods_id']));
				// }
				// $yuming = $_SERVER['HTTP_HOST'];
				// $urls = array(
				//     'http://www.example.com/1.html',
				//     'http://www.example.com/2.html',
				// );
				$api = 'http://data.zz.baidu.com/urls?site=www.gzshuhua.com&token='.$one['value'];
				$ch = curl_init();
				$options =  array(
				    CURLOPT_URL => $api,
				    CURLOPT_POST => true,
				    CURLOPT_RETURNTRANSFER => true,
				    CURLOPT_POSTFIELDS => implode("\n", $urls),
				    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
				);
				curl_setopt_array($ch, $options);
				$result = curl_exec($ch);
				// echo $result;
				$json = array('status'=>0,'message'=>'百度返回信息：'.$result);
				echo json_encode($json);exit;
			}
		}
		// echo json_encode($json);return;
	}

	function sitemap(){
		$site = new Sitemap;
		//首页
		$home = 'http://'.$_SERVER['HTTP_HOST'];
		$site->AddItem($home,1);
		//文章栏目
		$akwhere['is_hide'] = 0;
		$articlekind = db('article_kind')->field('kind_id,kind_name,parent_id')->where($akwhere)->select();
		foreach($articlekind as $value){
			// $urls = $home.url('Home/Index/category',array('id'=>$value['kind_id']));
			$urls = 'http://'.$_SERVER['HTTP_HOST'].'/category/'.$value['kind_id'].'.html';
			$site->AddItem($urls,1);
		}
		//文章
		$awhere['is_del'] = 0;
		$awhere['is_hide'] = 0;
		$article = db('article')->field('article_id,title,kind_id')->where($awhere)->limit(2000)->select();
		foreach($article as $value){
			// $urls = $home.url('Home/Index/article',array('id'=>$value['article_id']));
			$urls = 'http://'.$_SERVER['HTTP_HOST'].'/article/'.$value['article_id'].'.html';
			$site->AddItem($urls,1);
		}
		//商品栏目
		// $pro = 'http://'.$_SERVER['HTTP_HOST'].U('Product/product_list');
		// $site->AddItem($pro, 1);
		$pkwhere['is_hide'] = 0; 
		$goodskind = db('product_kind')->field('kind_id,kind_name')->where($pkwhere)->select(); 
		foreach ($goodskind as $value) {
			// $urls = 'http://'.$_SERVER['HTTP_HOST'].U('Product/product_list',array('id'=>$value['id']));
			$urls = 'http://'.$_SERVER['HTTP_HOST'].'/products/'.$value['kind_id'].'.html';
		    $site->AddItem($urls, 1);
		}
		//商品
		$pwhere['is_hide'] = 0;
		$product = db('product')->field('id,title')->where($pwhere)->limit(2000)->select(); 
		foreach ($product as $val) {
			// $urls = 'http://'.$_SERVER['HTTP_HOST'].U('Product/product',array('id'=>$val['id']));
			$urls = 'http://'.$_SERVER['HTTP_HOST'].'/product/'.$val['id'].'.html';
		    $site->AddItem($urls, 1);
		}

		$site->SaveToFile('sitemap.xml');
		$json = array('status'=>1,'message'=>'成功');
		echo json_encode($json);exit;
	}



}